/**
 * Tree Node class
 *
 */
function TreeNode( value ){
	
	var _value = value;
	var _left = null;
	var _right = null;
	var _parent = null;
	
	// right
	/**
	 * Set Right node
	 */
	this.setRight = function(node){
		if (  ! node instanceof TreeNode ){
			console.log('TreeNode.setRight only accepts TreeNode as an argument');
			return;
		}
		_right = node;
	}
	/**
	 * Get Right Node
	 */
	this.getRight = function(){return _right}
	/**
	 * Remove Right node
	 */
	this.removeRight = function(){
		_right = null;
	}
	
	// left
	/**
	 * Set Left node
	 */
	this.setLeft = function(node){
		if ( ! node instanceof TreeNode){
			console.log('TreeNode.setLeft only accepts TreeNode as an argument');
			return;
		}
		_left = node;
	}
	/**
	 * Get Left Node
	 */
	this.getLeft = function(){return _left}
	/**
	 * Remove left node
	 */
	this.removeLeft = function(node){
		_left = null;
	}
	
	// Public value getter
	this.getValue = function(){return _value}
}

ArrayTree = function( values ){
	/**
	 * array of value representing tree
	 * @var array
	 */
	this.values = values;
	/**
	 * Pointer to root node
	 * @var int
	 */
	this.root = ( values && (values.length > 0)) ? values[0]: null;
}

ArrayTree.prototype.walk = function( startPos ){
	
	if ( typeof startPos !== "number" ){
		startPos = 0;
	}
	
	var leftPos = this.getLeftPosition( startPos ),
		rightPos = this.getRightPosition( startPos ),
		nodeValue = this.values[startPos],
		leftValue = this.values[leftPos],
		rightValue = this.values[rightPos];
	
	return {
		val: nodeValue,
		left: ( leftValue !== undefined ) ? this.walk(leftPos) : null,
		right: ( rightValue !== undefined ) ? this.walk(rightPos) : null
	}
	
}

ArrayTree.prototype.getLeftPosition = function( nodePos ){
	return nodePos = (2 * nodePos) + 1;
}

ArrayTree.prototype.getRightPosition = function( nodePos ){
	return nodePos = (2 * nodePos) + 2;
}


function DomTree(  ){
	this.queue = [];
}

DomTree.walk = function( node ){
	if ( !( node instanceof HTMLElement )){
		throw new Error('root must be a dom node');
	}
	this.queue.push( node );
	var level = 1;
	while( this.queue.length > 0 ){
		console.log('walking level ' + level);
		var currentNode = this.queue.shift(),
			numChildren = currentNode.children.length;
		for ( var i=0; i < numChildren; i++ ){
			this.queue.push( currentNode.children[i]);
		}
		level++;
	}
}